package com.demo.bookshelf;

import com.demo.common.model.Bookshelf;
import com.demo.common.model.User;
import com.demo.user.UserService;
import com.demo.user.UserValidator;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;

@Before(BookShelfInterceptor.class)
public class BookShelfController extends Controller {

    @Inject
    BookShelfService bookShelfService;

    public void index() {
        Kv cond=Kv.create();
        if(StrKit.notBlank(getPara("bookshelfno"))){
            cond.set("bookshelfno",getPara("bookshelfno"));
        }
        setAttr("bookShelfPage", bookShelfService.paginate(getParaToInt(0, 1), 10,Kv.by("cond",cond)));
        render("bookshelf.html");
    }

    public void add() {
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(BookShelfValidator.class)
    public void save() {
        getBean(Bookshelf.class).save();
        redirect("/bookshelf");
    }

    public void edit() {
        setAttr("bookshelf", bookShelfService.findById(getParaToInt()));
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(BookShelfValidator.class)
    public void update() {
        getBean(Bookshelf.class).update();
        redirect("/bookshelf");
    }

    public void delete() {
        bookShelfService.deleteById(getParaToInt());
        redirect("/bookshelf");
    }
}
